import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {
  userinfo: {
    islogin: false,
    id: 0,
    username: '',
    age: 0,
    sex: '',
    note: '',
    registerdate: ''
  }
}

const mutations = {
  SETINFO (state, payload) {
    state.userinfo.islogin = true
    state.userinfo.id = payload.id
    state.userinfo.username = payload.username
    state.userinfo.age = payload.age
    state.userinfo.sex = payload.sex
    state.userinfo.note = payload.note
    state.userinfo.registerdate = payload.registerdate
  },
  RESETINFO (state) {
    state.userinfo.islogin = false
    state.userinfo.id = 0
    state.userinfo.username = ''
    state.userinfo.age = 0
    state.userinfo.sex = ''
    state.userinfo.note = ''
    state.userinfo.registerdate = ''
  }
}

const actions = {
  login ({commit}, info) {
    commit('SETINFO', info)
  },
  logout ({commit}) {
    commit('RESETINFO')
  }
}

const getters = {
  userinfo: function () {
    return state.userinfo
  }
}

export default new Vuex.Store({
  state, // 状态对象
  mutations, // 包含多个更新state函数对象
  actions, // 包含多个对应事件回调函数的对象
  getters // 包含多个getter计算属性函数的对象
})
